import { defineStore } from 'pinia'
import { iChannelItem, IResponse } from '../types'
import request from '../utils/request'

//创建channels模块并导出
export default defineStore('channels', {
  state() {
    return {
      channels: [] as iChannelItem[], //断言
      active: -1,
    }
  },
  actions: {
    async getChannelList() {
      //get的泛型参数约定的是r.data的类型

      const r = await request.get<IResponse<{ channels: iChannelItem[] }>>(
        '/channels'
      )
      console.log(r.data.data.channels)
      this.channels = r.data.data.channels //后端返回的数据存储到channels
      this.active = r.data.data.channels[0].id //默认第一个高亮
    },
    //
    changeActive(id: number) {
      this.active = id
    },
  },
})
